// view/index/zhengcfgxq/zhengcfgxq.js
let app = getApp()
let yuming = app.globalData.yuming
var shuzong = []
var shu = []
const request = require('../../config/request');
Page({
  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    heights: "",
    title: '培训记录',
    zheng: [],
    hangyeID: '',
    page: 1,
    fen_gao: '',
    fen_index: 0,
    list_gao: '',
    zheng_wu: false,
    peixin_list: [],
    total:''
  },
  gojians(e) {
    let url = e.currentTarget.dataset.fil
    var that = this
    var s = url.lastIndexOf(".")
    var b = url.substring(s + 1) //下划线之后
    console.log(b)
    if (b == 'jpg' || b == 'jpeg' || b == 'png') {
        wx.previewImage({
            urls: [url]
        })
    } else {
        wx.showLoading({
            title: '正在打开中',
        })
        wx.downloadFile({
            url: url,
            success(res) {
                var filePath = res.tempFilePath
                console.log(url)
                wx.openDocument({
                    filePath: filePath,
                    fileType: b,
                    success: function (res) {
                        wx.hideLoading({
                            success: (res) => {},
                        })
                    },
                    fail(err) {
                        console.log(err)
                        wx.hideLoading({
                            success: (res) => {},
                        })
                        wx.showToast({
                            title: '打开失败',
                            icon: 'none'
                        })
                    }
                })
            },
            fail(err) {
                console.log(err)
                wx.hideLoading({
                    success: (res) => {},
                })
                wx.showToast({
                    title: '打开失败',
                    icon: 'none'
                })
            }
        })
    }


},
  // 删除
  shanchu(e) {
    console.log(e)
    var id = e.currentTarget.dataset.id
    console.log(id)
    var that = this
    let title = ''
    wx.showModal({
      title: '您确定要删除吗？',
      cancelText: '取消',
      confirmText: '确定',
      success: function (res) {
        if (res.confirm) {
          that.dele(id)
        } else {
          console.log("取消")
        }
      }
    })
  },
  dele(id) {
    let that = this
    this.myRequest(yuming + 'Shop/shop_training_remove', {
      id: id,
      shopId: wx.getStorageSync('shopId')
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      if (res.data.code == 200) {
        that.wenz()
      }
    })
  },
  // 多组放大
  fangda(e) {
    var img = e.currentTarget.dataset.img
    var list = e.currentTarget.dataset.list
    wx.previewImage({
      current: img,
      urls: list
    })
  },
  // 添加培训
  addpei() {
    wx.navigateTo({
      url: '/wenshidu/addpeixun/addpeixun',
    })
  },
  xq(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/page_other/zhengcfgxqs/zhengcfgxqs?id=' + id,
    })
  },
  // 查看更多
  lookmore() {
    // shuzong = this.data.tsyList
    shuzong = this.data.peixin_list
    this.setData({
      page: (this.data.page) + 1
    })
    this.wenz();
  },
  xlaaa(e){
    console.log(e)
    var index = e.currentTarget.dataset.index
    var item = this.data.peixin_list
    item[index].iszhan = !item[index].iszhan
    console.log(item[index].iszhan)
    this.setData({
      peixin_list:item
    })
  },
  // 文章、boss圈
  wenz() {
    var that = this
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    this.myRequest(yuming + 'Shop/shop_training_list', {
      shopId: wx.getStorageSync('shopId'),
      page: that.data.page
    }, 'POST').then(res => {
      wx.hideLoading();
      that.setData({
        total:res.data.total
      })
      if (res.data.code == 200) {
        wx.hideLoading();
        shu = res.data.data
        shu.forEach(item => {
          item.iszhan = true
        })
        if (that.data.page != 1) {
          if (shu.length != 10) {
            that.setData({
              zheng_wu: true
            })
          }
        }
        that.setData({
          peixin_list: shuzong.concat(shu)
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 0
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5,
      title: options.title,
      list_gao: wx.getMenuButtonBoundingClientRect().top + 40
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      hangyeID: wx.getStorageSync('stryId')
    })
    this.wenz()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})